package com.itheima.dao;

import com.itheima.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


public interface IUserDao {

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Results(id = "userInfoMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,
                    many=@Many(select = "com.itheima.dao.IRoleDao.findRoleByUserId")),
    })
    @Select("select * from users where username = #{username}")
    public UserInfo findByUsername(String username);


    /**
     * 查询所有用户
     * @return
     */
    @Select("select * from users")
    public List<UserInfo> findAll();


    /**
     * 添加用户
     * @param userInfo
     */
    @Insert("insert into users(email,username,password,phoneNum,status) " +
            "values(#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(UserInfo userInfo);


    /**
     * 根据用户id查询用户详情
     * @param id
     * @return
     */
    @ResultMap("userInfoMap")
    @Select("select * from users where id = #{id}")
    UserInfo findUserById(String id);

    /**
     * 根据id给用户添加角色
     * @param userId
     * @param roleId
     */
    @Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId})")
    void addRoleToUser(String userId, String roleId);
}
